package com.xb.loan.util.strategy;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 
 * 水平分表策略工厂
 */
public class TableNoAllocatorFactory {

    private TableNoAllocatorFactory() {
    };

    private static Map<String, String> acllocatorMapping = new HashMap<String, String>();

    private static Map<String, TableNoAllocator> allocatorMap = new ConcurrentHashMap<String, TableNoAllocator>();

    static {
        acllocatorMapping.put("t_user_info", "com.palm.plat.util.common.strategy.DefaultTableNoAllocator");
        acllocatorMapping.put("t_cash_bill", "com.palm.plat.util.common.strategy.DefaultTableNoAllocator");
        acllocatorMapping.put("t_user_msg", "com.palm.plat.util.common.strategy.DefaultTableNoAllocator");
        acllocatorMapping.put("t_user_log", "com.palm.plat.util.common.strategy.DefaultTableNoAllocator");
    }
    
    
    /**
     * 
     * 根据表名获取对应的表索引分配器
     * @param tableName
     * @return
     */
    public static TableNoAllocator getAllocator(String tableName) {
        if (!allocatorMap.containsKey(tableName) && (acllocatorMapping.containsKey(tableName))) {
            {
                try {
                    allocatorMap.put(tableName, (TableNoAllocator) Class.forName( acllocatorMapping.get(tableName)).newInstance());
                } catch (Exception e) {
                }
            }
        }
        return allocatorMap.get(tableName);
    }
    
    public static void main(String[] args) {
       String tableNo1= TableNoAllocatorFactory.getAllocator("t_user_info").getTableNo("00120");
       String tableNo2= TableNoAllocatorFactory.getAllocator("t_cash_bill").getTableNo("00124");
       System.out.println(tableNo1);
       System.out.println(tableNo2);
    }

}
